var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var session = require('express-session');
var MongoStore = require('connect-mongo')(session);

var https = require('https'),
	fs = require('fs');
//https options
/*
var options = {
	key : fs.readFileSync('./key/p.key').toString(),
	cert : fs.readFileSync('./key/server.cer').toString(),
	ca : fs.readFileSync('./key/server.req').toString()
}
*/

var settings = require('./settings');
require('./modules/connectDB.js');

var routes = require('./routes/index');
var student = require('./routes/student');
var teacher = require('./routes/teacher');
var school = require('./routes/school');
var admin = require('./routes/admin');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(session({
	secret:settings.cookie_secret,
	key:settings.db_name,
	store:new MongoStore({
		db:"nodejs",
		host:process.env.OPENSHIFT_MONGODB_DB_HOST || '127.0.0.1',
		username:process.env.OPENSHIFT_MONGODB_DB_USERNAME,
		password:process.env.OPENSHIFT_MONGODB_DB_PASSWORD,
	})
}));



app.use(express.static(path.join(__dirname, 'public')));

//routes
app.get('/', routes);
app.get('/test', routes);
app.get('/err',routes);
app.all('/student/*', student);
app.all('/student', student);
app.all('/teacher/*',teacher);
app.all('/teacher',teacher);
app.all('/school',school);
app.all('/school/*',school);
app.all('/admin',admin);
app.all('/admin/*',admin);
app.all('/logout',admin);
app.all('/key/*',teacher);

/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

/// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});



/*
#!/usr/bin/env node
var app = require('../app');
var debug = require('debug')('signup');
*/
var debug = require('debug')('signup');
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 3000);
app.set('add',process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');

/*
var server = app.listen(app.get('port'),app.get('add'), function() {
	debug('Express server listening on port ' + server.address().port);
});
*/


// start https server and http server
/*
https.createServer(options,app).listen(443,function(){
	console.log('https server created!');
})
*/

app.listen(3000);

module.exports = app;
